const BASE_URL = 'http://192.168.110.192:9003'


// 系统常量
const SYSTEM = {
	VERSION_NUMBER: uni.getSystemInfoSync().appVersion,
}

// 请求拦截器
const request = (options) => {
	return new Promise((resolve, reject) => {
		// 获取存储的 user_id
		const user_id = uni.getStorageSync('user_id')
		
		// 合并请求头，如果有 user_id 就带上
		const header = {
			'Content-Type': 'application/x-www-form-urlencoded',
			...options.header
		}
		if (user_id) {
            // 判断有没有data
			if(options.data) {
				options.data['user_id'] = user_id
			} else {
				options.data = {
					user_id: user_id
				}
			}
		}
		
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: options.data,
			header: header,
			success: (res) => {
				if (res.statusCode === 200) {
					resolve(res.data)
				} else {
					reject(res)
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

// API 接口
export const api = {
	// 登录
	login: (data) => {
		return request({
			url: '/api/user/login/',
			method: 'POST',
			data
		})
	},
	
	// 注册
	register: (data) => {
		return request({
			url: '/api/user/register/',
			method: 'POST',
			data
		})
	},
    request: (options) => {
		return request(options)
	},
    BASE_URL: BASE_URL,
	SYSTEM: SYSTEM
	
	// 其他接口...
} 